﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace EvidenceManagement.Logic.Security
{
    public class MonitoredModule
    {
        List<MonitoredAction> monitoredActions = new List<MonitoredAction>();
        readonly Type moduleType;

        /// <summary>
        /// Initializes a new instance of the <see cref="MonitoredModule"/> class.
        /// </summary>
        public MonitoredModule()
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MonitoredModule"/> class.
        /// </summary>
        /// <param name="moduleName">Name of the module.</param>
        /// <param name="moduleType">Type of the module.</param>
        public MonitoredModule(string moduleName, Type moduleType)
        {
            Name = moduleName;
            this.moduleType = moduleType;
            TypeName = ModuleType.FullName;
            AssemblyQualifiedName = ModuleType.AssemblyQualifiedName;            
        }

        /// <summary>
        /// Gets or sets the name of the module.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get;
            set;
        }

        /// <summary>
        /// Gets the type of the module.
        /// </summary>
        /// <value>The type of the module.</value>
        [XmlIgnore()]
        public Type ModuleType
        {
            get { return moduleType; }
        }

        /// <summary>
        /// Gets or sets the name of the type.
        /// </summary>
        /// <value>The name of the type.</value>
        public string TypeName { get; set; }

        /// <summary>
        /// Gets or sets the assembly qualified name.
        /// </summary>
        /// <value>The assembly qualified name.</value>
        public string AssemblyQualifiedName { get; set; }

        /// <summary>
        /// Gets or sets a list of the monitored actions.
        /// </summary>
        /// <value>The monitored actions.</value>
        public MonitoredAction[] MonitoredActions
        {
            get
            {
                return monitoredActions.ToArray();
            }
            set
            {
                monitoredActions = value.ToList();
            }
        }

        internal void AddAction(MonitoredAction action)
        {
            if (!monitoredActions.Exists(a => a.ActionName == action.ActionName
                && a.PermissionType == action.PermissionType))
            {
                monitoredActions.Add(action);
            }
        }

        internal void AddActionList(List<MonitoredAction> actions)
        {
            var filterdAction = actions.Where(a => !monitoredActions.Exists(e => e.ActionName == a.ActionName
                && e.PermissionType == a.PermissionType));

            monitoredActions.AddRange(filterdAction);
        }
        
    }
}
